<!-- 项目管理 -->
<template>
  <div style="padding-left:20px;">
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item>
        <el-input v-model="data.name" size="medium" class="iup_width200" placeholder="请输入项目名称"></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="data.sn" size="medium" class="iup_width200" placeholder="请输入项目编号"></el-input>
      </el-form-item>
      <el-form-item>
        <el-select v-model="data.status" size="medium" class="iup_width200" placeholder="请选择状态">
          <el-option v-for="item in statusList" :key="item.code" :label="item.name" :value="item.code"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="medium" @click="search">查询</el-button>
        <el-button type="info" size="medium" @click="reset">重置</el-button>
      </el-form-item>
      <el-form-item class="flr">
        <el-button type="primary" size="medium" @click="add('create','')">添加</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="getList" v-loading="loading" element-loading-text="数据加载中" empty-text border stripe>
      <div slot="empty">
        <div v-if="loading"></div>
        <div v-else>暂无数据</div>
      </div>
      <el-table-column type="index" label="序号" align="center" width="70"></el-table-column>
      <el-table-column prop="sn" label="项目编号" align="center" width="220"></el-table-column>
      <el-table-column prop="name" label="项目名称" align="center" width="150"></el-table-column>
      <el-table-column prop="totalAmt" label="总金额" align="center" width="120">
        <template slot-scope="scope">
          <span class="green">{{common.formatAmount(scope.row.totalAmt,2)}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="minAmt" label="起投金额" align="center" width="120">
        <template slot-scope="scope">
          <span class="green">{{common.formatAmount(scope.row.minAmt,2)}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="availableAmt" label="已投金额" align="center" width="120">
        <template slot-scope="scope">
          <span class="green">{{common.formatAmount(scope.row.availableAmt,2)}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="totalIncomeAmt" label="项目总收益" align="center" width="120">
        <template slot-scope="scope">
          <span class="green">{{common.formatAmount(scope.row.totalIncomeAmt,2)}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="term" label="投资期限" align="center" width="120">
        <template slot-scope="scope">
          <span class="red">
            {{scope.row.term}}
            {{scope.row.termUnitName}}
          </span>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态" align="center" width="120">
        <template slot-scope="scope">
          <span :class="{green:scope.row.status=='2',red:scope.row.status=='1'}">{{scope.row.status==2?'已完成':'进行中'}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="addTime" label="创建时间" :formatter="common.formatTime" align="center" width="170"></el-table-column>
      <el-table-column prop="dueTime" label="截至时间" :formatter="common.formatTime" align="center" width="170"></el-table-column>
      <el-table-column label="操作" min-width="300" fixed="right" align="center">
        <template slot-scope="scope">
          <el-button type="primary" size="small" @click="add('edit',scope.row)" v-show="scope.row.status =='1'">修改</el-button>
          <el-button type="primary" size="small" @click="complete(scope.row)" v-show="scope.row.status =='1'">完成</el-button>
          <el-button type="primary" size="small" @click="goTeam(scope.row)">团队管理</el-button>
          <el-button type="danger" size="small" @click="del(scope.row)" v-show="scope.row.status =='1'">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页开始 -->
    <pagination :total="total" :pageData="data" @hanleSize="changePage" @hanlePage="handleSizeChange"></pagination>

    <!-- 添加/删除 -->
    <projectListModal v-if="roleData.showModal" :roleData="roleData" @close="getRoleList" />

    <!-- 项目收益 -->
    <completeModal v-if="completeData.showModal" :completeData="completeData" @close="getRoleList"></completeModal>
  </div>
</template>
<script>
import projectListModal from "component/permissions/projectListModal";
import completeModal from "component/permissions/completeModal";
export default {
  components: {
    projectListModal,
    completeModal
  },
  data () {
    return {
      type: "PROJECT_STATUS",
      getList: [],
      data: {
        pageSize: 10,
        currentPage: 1,
        status: "",
        sn: "",
        name: ""
      },
      total: 0,
      loading: false,
      getTime: "",
      roleData: {}, // 新增修改
      completeData: {}, // 完成按钮
      statusList: []
    };
  },
  methods: {
    //跳转团队管理
    goTeam (row) {
      this.$router.push({ path: `/team/${row.id}` })
    },
    //获取字典表
    async getterm () {
      this.statusList = await this.common.enumsList(this.type);
    },
    //点击页数触发
    changePage (page) {
      this.data.currentPage = page;
      this.getRoleList();
    },
    //选择页数最多显示几条数据
    handleSizeChange (val) {
      this.data.pageSize = val;
      this.getRoleList();
    },
    //查询
    changeInput () {
      this.data.currentPage = 1;
      this.getTime();
    },
    //查询关键字
    search () {
      this.data.currentPage = 1;
      this.getRoleList();
    },
    //重置列表
    reset () {
      Object.assign(this.$data.data, this.$options.data().data); //重置列表
      this.getRoleList();
    },
    //获取列表
    async getRoleList () {
      this.loading = true;
      let url = "/project/queryPage";
      let result = await this.request.httpRequest("POST", url, this.data);
      if (result.code == 200) {
        this.total = result.data.totalRows;
        this.data.pageSize = result.data.pageSize;
        this.data.currentPage = result.data.currentPage;
        this.getList = result.data.datas;
      } else {
        this.$message({ type: "error", message: result.msg });
      }
      this.loading = false;
    },
    //是否要删除
    del (row) {
      this.$confirm(`您确定要删除此项目吗？`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(async () => {
        this.loading = true;
        let url = `/project/delete/${row.id}`;
        let result = await this.request.httpRequest("DELETE", url);
        if (result.code == 200) {
          this.$message({ type: "success", message: result.msg });
          this.getRoleList();
        } else {
          this.loading = false;
          this.$message({ type: "error", message: result.msg });
        }
      });
    },
    //新增、修改
    add (type, row) {
      this.roleData = {
        title: type == "create" ? "添加项目" : "修改项目",
        showModal: true,
        roleId: row.id || "",
        type: type
      };
    },
    // 完成
    complete (row) {
      this.completeData = {
        title: "完成项目",
        showModal: true,
        id: row.id
      };
    }
  },
  mounted () {
    this.getterm();
    this.getRoleList();
    this.getTime = this.helper.debounce(this.getRoleList); //防抖
  },
  created () {
    document.onkeydown = e => {
      if (e.keyCode == 13) {
        this.search();
      }
    };
  }
};
</script>
<style scoped>
.el-form {
  padding: 15px 0px !important;
}
.el-form-item {
  margin-bottom: 0px !important;
}
</style>